package com.lu.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.reggie.common.R;
import com.lu.reggie.entity.Employee;
import com.lu.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 况山云
 * @since 2022-08-20
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;


    /**
     * 登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        // 1.加密输入的密码
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(password);
        // 2.根据输入的用户名查询数据库
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //mp提供的比较方法
        lambdaQueryWrapper.eq(Employee::getUsername, employee.getUsername());
        //mp提供的查询单条记录
        Employee admin = iEmployeeService.getOne(lambdaQueryWrapper);
        System.out.println(admin);
        // 3.查询有无该用户
        if (null == admin) {
            return R.error("该用户不存在!");
        }
        // 4.密码不正确
        if (!admin.getPassword().equals(password)) {
            return R.error("密码不正确!");
        }
        // 5.查看状态
        if (admin.getStatus() == 0) {
            return R.error("该账号已被禁用!");
        }
        //登陆成功,存储到session中
        HttpSession session = request.getSession();
        session.setAttribute("employee", admin.getId());
        return R.success(admin);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功!");
    }

    /**
     * 无论登录，注册，添加，正常开发都需要对输入的密码进行加密
     * 该项目未使用security安全框架，所以现在使用工具类加密
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工,信息为:{}", employee.toString());
        //根据前端页面需求，需要给每个新增的员工设置默认初始密码123456,因为该系统是无法自己注册的，只能由管理员添加账号
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*//部分信息，如添加和修改时间，添加和修改者信息需要自己添加
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //拿到修改者存在session中的id
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));*/
        //调用service添加方法
        iEmployeeService.save(employee);
        //返回信息
        return R.success("添加成功!");
    }


    /**
     * 员工列表分页，条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //分页构造器
        Page pages = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //过滤
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //降序
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //分页查询
        iEmployeeService.page(pages, queryWrapper);
        //返回信息
        return R.success(pages);
    }


    /**
     * 通用修改员工信息
     * 账户冻结
     * @param request
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        /*Long ID = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(ID);*/
        iEmployeeService.updateById(employee);
        return R.success("修改成功!");
    }

    /**
     * 根据ID查询员工
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id) {
        log.info("根据ID查询员工");
        Employee employee = iEmployeeService.getById(id);
        if (null != employee) {
            return R.success(employee);
        }
        return R.error("该用户不存在!");
    }



}
